﻿using System.Collections.Generic;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Interfaces;
using Dost.Khcn.Model;

namespace Dost.Khcn.Data.Implements
{
    public class ChuyenGiaOnlineDao : IChuyenGiaOnlineDao
    {
        public int SearchChuyenGiaOnlineCount(SearchChuyenGiaOnlineModel searchModel)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var result = db.SearchChuyenGiaOnlineCount(searchModel.HoTen, searchModel.TenDonVi, searchModel.TenCongTrinh,
                    searchModel.LinhVucId, searchModel.HocHamId,
                    searchModel.HocViId).FirstOrDefault();

                return result ?? 0;
            }
        }

        public List<SearchChuyenGiaOnline_Result> SearchChuyenGiaOnline(int startIndex, int pageSize, string sorting, SearchChuyenGiaOnlineModel searchModel)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.SearchChuyenGiaOnline(startIndex, pageSize, sorting, searchModel.HoTen, searchModel.TenDonVi, searchModel.TenCongTrinh,
                    searchModel.LinhVucId, searchModel.HocHamId,
                    searchModel.HocViId).ToList();
            }
        }

        public int SearchChuyenGiaSimpleCount(SearchChuyenGiaSimpleModel searchModel)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var result = db.SearchChuyenGiaSimpleCount(searchModel.HoTen, searchModel.TenDonVi, searchModel.DiaChiDonvi,
                    searchModel.LinhVucId, searchModel.NhomLinhVucId).FirstOrDefault();

                return result ?? 0;
            }
        }

        public List<SearchChuyenGiaSimple_Result> SearchChuyenGiaSimple(int startIndex, int pageSize, string sorting, SearchChuyenGiaSimpleModel searchModel)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.SearchChuyenGiaSimple(startIndex, pageSize, sorting, searchModel.HoTen, searchModel.TenDonVi, searchModel.DiaChiDonvi,
                    searchModel.LinhVucId, searchModel.NhomLinhVucId).ToList();
            }
        }

        public int SearchChuyenGiaAdvanceCount(SearchChuyenGiaAdvanceModel searchModel)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var result = db.SearchChuyenGiaAdvanceCount(searchModel.HoTen, searchModel.TenDonVi, searchModel.DiaChiDonvi,
                    searchModel.LinhVucId, searchModel.NhomLinhVucId, searchModel.TenLuanAn, searchModel.TenCongTrinh).FirstOrDefault();

                return result ?? 0;
            }
        }

        public List<SearchChuyenGiaAdvance_Result> SearchChuyenGiaAdvance(int startIndex, int pageSize, string sorting, SearchChuyenGiaAdvanceModel searchModel)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.SearchChuyenGiaAdvance(startIndex, pageSize, sorting, searchModel.HoTen, searchModel.TenDonVi, searchModel.DiaChiDonvi,
                    searchModel.LinhVucId, searchModel.NhomLinhVucId, searchModel.TenLuanAn, searchModel.TenCongTrinh).ToList();
            }
        }

        public ChuyenGiaDetailView GetInfoDetailsById(int id)
        {
            ChuyenGiaDetailView chuyenGiaInfo;

            using (var db = new NLKHCNDataEntities())
            {
                chuyenGiaInfo = db.ChuyenGiaDetailViews.FirstOrDefault(x => x.Id == id);
                if (chuyenGiaInfo == null) return null;

                chuyenGiaInfo.QuaTrinhCongTacList = db.QuaTrinhCongTacs.Where(x => x.ChuyenGiaId == id).ToList();
                chuyenGiaInfo.CongTrinhNghienCuuList = db.CongTrinhNghienCuus.Where(x => x.ChuyenGiaId == id).ToList();
                chuyenGiaInfo.DeTaiCapNhaNuocList = db.DeTaiCapNhaNuocViews.Where(x => x.ChuyenGiaId == id).ToList();
                chuyenGiaInfo.DeTaiCapTinhList = db.DeTaiCapTinhViews.Where(x => x.ChuyenGiaId == id).ToList();
                chuyenGiaInfo.GiaiThuongKhcnList = db.GiaiThuongKhcns.Where(x => x.ChuyenGiaId == id).ToList();
                //chuyenGiaInfo.FileList = db.ChuyenGiaFileViews.Where(x => x.ChuyenGiaId == id).ToList();
            }

            return chuyenGiaInfo;
        }
    }
}
